Typical computing devices support execution of binary code including instructions targeted to a particular instruction set architecture (ISA). Binary translation systems generate a translated binary based on an original or native binary. Binary translation may be used to execute a binary targeted to a particular ISA on a computing device that supports a different ISA, without recompiling the original binary. Additionally or alternatively, binary translation may be used to take advantage of new instructions or other features supported by a particular computing device but not included in the original binary, to increase performance through dynamic optimization, to enforce security policies, or for other purposes.
Most processors support native call and return instructions, which are used to perform subroutine calls and returns and are very common in compiled binaries. Many processors include specialized hardware used to optimize calls and returns, such as a stack-based return prediction hardware (e.g., a return stack buffer). Many binary translation systems cannot directly use native call and return instructions without breaking compatibility, and thus emulate call and return instructions with jump instructions. However, using jump instructions may not take advantage of optimized call/return hardware of the processor. To allow the use of native call and return instructions, some binary translation systems maintain a shadow stack in memory. However, typical shadow stack implementations require several expensive memory load and/or store instructions to switch between the native stack and the shadow stack. For example, a typical implementation of a translated call routine may execute four load/store operations: storing the value of the stack pointer to a native stack save area, loading the value of the stack pointer from a shadow stack save area, perform the call instruction, storing the new value of the stack pointer to the shadow stack save area, and loading the value of the stack pointer from the native stack save area.